<?php


namespace app\auth\service;


use app\common\enums\DelFlagEnum;
use think\db\exception\PDOException;
use think\facade\Log;

class BaseService
{

    public $pageSize = 20;
    public $company = 0;
    public $successCode = 200;
    public $successMsg = '获取数据成功';
    protected $model;
	public $operateLogService;
	public $userId;
    public function __construct()
    {
    	// 获取当前用户所属的企业ID
        $admin = session('admin');
        if($admin) {
			$this->userId = $admin['id'];
		}
    }

    /**
     * 添加
     * @param $params
     * @return mixed
     */
    public function add($data)
    {
    	if(isset($data['file'])) {
			unset($data['file']);
		}
        $id = $this->model->insertGetId($data);
        if($id > 0) {
            return ['code' => 0,'msg' => '操作成功','data' => $id];
        }else {
            return ['code' => 1,'msg' => '操作失败','data' => 0];
        }
    }

    /**
     * 查看详情
     * @return array
     */
    public function detail($id)
    {
        $data = $this->model->where(['id' => $id])->find();
        return $data;
    }

    /**
     * @param array $data
     * @return array
     */
    public function modify($where,$data)
    {
		if(isset($data['file'])) {
			unset($data['file']);
		}
        $res = $this->model->where($where)->save($data);
        if($res !== false) {
            return ['code' => 0,'msg' => '操作成功'];
        }else {
            return ['code' => 1,'msg' => '操作失败'];
        }
    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id) {
        try {
            $this->model->where(['id' => $id])->data(['del_flag' => DelFlagEnum::Yes])->save();
            return ['code' => 0,'msg' => '操作成功'];
        }catch (PDOException $e) {
            Log::error('删除失败,错误信息:'.$e->getMessage());
            return ['code' => 1,'msg' => '操作失败'];
        }
    }

    /**
     * 初始化查询条件
     * @return array[]
     */
    public function initCondition() {
        $condition = [
            ['del_flag','=', DelFlagEnum::No]
        ];
        return $condition;
    }

}